package com.ktjy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktjy.entity.Auction;
import com.ktjy.mapper.AuctionMapper;
import com.ktjy.service.AuctionService;
import com.ktjy.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author: DABINGCHNEGJIAO
 * @CreateTime: 2025-06-15
 * @Description:
 * @Version: 1.0
 */
public class AuctionServiceImpl implements AuctionService {


    @Override
    public PageInfo<Auction> selectAuctionuser(Auction auction, Integer pageNum, Integer pageSize) {
        try (SqlSession sqlSession = MyBatisUtil.createSqlSession()) {
            AuctionMapper mapper = sqlSession.getMapper(AuctionMapper.class);
            PageHelper.startPage(pageNum, pageSize);
            List<Auction> auctions = mapper.selectAuctionuser(auction);
            PageInfo<Auction> pageInfo = new PageInfo<>(auctions);

            return pageInfo;
        }
    }

    @Override
    public boolean save(Auction auction) {
        try (SqlSession sqlSession = MyBatisUtil.createSqlSession()) {
            try {
                if (auction.getAuctionId() != null) {
                    AuctionMapper mapper = sqlSession.getMapper(AuctionMapper.class);
                    int i = mapper.updateAuction(auction);
                    sqlSession.commit();
                    return i > 0;
                } else {
                    AuctionMapper mapper = sqlSession.getMapper(AuctionMapper.class);
                    int i = mapper.insertAuction(auction);
                    sqlSession.commit();
                    return i > 0;
                }
            } catch (Exception e) {
                sqlSession.rollback();
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public Auction selectAuctionById(int auctionid) {
        try (SqlSession sqlSession = MyBatisUtil.createSqlSession()) {
            AuctionMapper mapper = sqlSession.getMapper(AuctionMapper.class);
            return mapper.selectAuctionById(auctionid);
        }
    }

    @Override
    public boolean deleteAuctionById(int auctionid) {
        try (SqlSession sqlSession = MyBatisUtil.createSqlSession()) {
            try {
                AuctionMapper mapper = sqlSession.getMapper(AuctionMapper.class);
                int i = mapper.deleteAuctionById(auctionid);
                sqlSession.commit();
                return i > 0;
            } catch (Exception e) {
                sqlSession.rollback();
                e.printStackTrace();
                return false;
            }
        }
    }
}
